<?php

declare(strict_types=1);
/**
 * Author: Zane
 * Email: 873934580@qq.com
 * Date: 2023/5/24.
 */

namespace App\Http\Controllers\System;

use App\Exceptions\CommonExceptionConstants;
use App\Exceptions\ServiceException;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Traits\IndexTrait;
use App\Http\Request\System\SystemRequest;
use App\Models\System;
use App\Models\SystemLog;
use App\Service\PermissionService;
use App\Utils\ResultsVo;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;

class SystemController extends Controller
{
    use IndexTrait;

    protected $permissionService;

    public function __construct(PermissionService $permissionService)
    {
        parent::__construct();
        $this->permissionService = $permissionService;
    }

    public function store(SystemRequest $request): \Illuminate\Http\JsonResponse
    {
        $data = $request->all();
        $data['password'] = Hash::make(trim($data['password']));
        (new System())->fill($data)->save();

        return ResultsVo::success();
    }

    public function edit(SystemRequest $request, $id): \Illuminate\Http\JsonResponse
    {
        $data = $request->all();
        if (isset($data['password'])) {
            if ($data['password']) {
                $data['password'] = Hash::make($data['password']);
            } else {
                unset($data['password']);
            }
        }
        $dao = System::query()->find($id);

        if (!$dao) {
            throw new ServiceException(CommonExceptionConstants::NO_FIND_DATA);
        }
        $dao->update($data);

        return ResultsVo::success();
    }

    public function myEdit(Request $request): \Illuminate\Http\JsonResponse
    {
        $user = Auth::guard(config('auth.app_guard_name'))->user();
        $data = $request->all();
        $password = $request->get('new_password');
        if ($password) {
            $data['password'] = Hash::make($password);
        }
        $user->update($data);

        return ResultsVo::success();
    }

    public function delete(): \Illuminate\Http\JsonResponse
    {
        $ids = $this->getIds();
        System::query()->where('is_founder', 0)->whereIn('id', $ids)->delete();

        return ResultsVo::success();
    }

    /**
     * 登录日志.
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function loginLog(): \Illuminate\Http\JsonResponse
    {
        $id = Auth::guard(config('auth.app_guard_name'))->id();
        $results = SystemLog::query()->select(['id', 'system_id', 'login_at', 'login_ip', 'status'])
            ->where('system_id', $id)
            ->with('system:id,nickname')
            ->orderBy('id', 'desc')->paginate()->toArray();

        return ResultsVo::success($results);
    }

    public function status(): \Illuminate\Http\JsonResponse
    {
        $ids = $this->getIds();
        System::query()->where('is_founder', 0)->whereIn('id', $ids)->update([
            'status' => (int) request()->get('value'),
        ]);

        return ResultsVo::success();
    }

    public function myPermission(): \Illuminate\Http\JsonResponse
    {
        return ResultsVo::success($this->permissionService->myPermission());
    }
}
